package org.jeecg.modules.demo.weixin.smtyuyuelist.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.demo.weixin.entity.SmtTrkList;
import org.jeecg.modules.demo.weixin.entity.SmtYuyueList;
import org.jeecg.modules.demo.weixin.entity.SmtYuyueUser;
import org.jeecg.modules.demo.weixin.smttrklist.service.ISmtTrkListService;
import org.jeecg.modules.demo.weixin.smtyuyuelist.service.ISmtYuyueListService;
import org.jeecg.modules.demo.weixin.smtyuyueuser.service.ISmtYuyueUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: smt_yuyue_list
 * @Author: jeecg-boot
 * @Date: 2023-10-23
 * @Version: V1.0
 */
@Api(tags = "smt_yuyue_list")
@RestController
@RequestMapping("/weixin/smtyuyuelist/smtYuyueList")
@Slf4j
public class SmtYuyueListController extends JeecgController<SmtYuyueList, ISmtYuyueListService> {
    @Autowired
    private ISmtYuyueListService smtYuyueListService;
    @Autowired
    private ISmtTrkListService smtTrkListService;
    @Autowired
    private ISmtYuyueUserService smtYuyueUserService;


    /**
     * 分页列表查询
     *
     * @param smtYuyueList
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @ApiOperation(value = "smt_yuyue_list-分页列表查询", notes = "smt_yuyue_list-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<SmtYuyueList>> queryPageList(SmtYuyueList smtYuyueList,
                                                     @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                     @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                     HttpServletRequest req) throws ParseException {
//		QueryWrapper<SmtYuyueList> queryWrapper = QueryGenerator.initQueryWrapper(smtYuyueList, req.getParameterMap());
//		if (StringUtils.isNotBlank(smtYuyueList.getMerId())){
//			queryWrapper.eq("merchant_id",smtYuyueList.getMerId());
//		}
//		queryWrapper.orderByDesc("create_time");

        Page<SmtYuyueList> page = new Page<SmtYuyueList>(pageNo, pageSize);
        IPage<SmtYuyueList> pageList = smtYuyueListService.queryMList(page, smtYuyueList);
        return Result.OK(pageList);
    }


    /**
     * 添加
     *
     * @param smtYuyueList
     * @return
     */
    @AutoLog(value = "smt_yuyue_list-添加")
    @ApiOperation(value = "smt_yuyue_list-添加", notes = "smt_yuyue_list-添加")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody SmtYuyueList smtYuyueList) {
        smtYuyueListService.save(smtYuyueList);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param smtYuyueList
     * @return
     */
    @AutoLog(value = "smt_yuyue_list-编辑")
    @ApiOperation(value = "smt_yuyue_list-编辑", notes = "smt_yuyue_list-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody SmtYuyueList smtYuyueList) {
        smtYuyueListService.updateById(smtYuyueList);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "smt_yuyue_list-通过id删除")
    @ApiOperation(value = "smt_yuyue_list-通过id删除", notes = "smt_yuyue_list-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        smtYuyueListService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "smt_yuyue_list-批量删除")
    @ApiOperation(value = "smt_yuyue_list-批量删除", notes = "smt_yuyue_list-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.smtYuyueListService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "smt_yuyue_list-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<SmtYuyueList> queryById(@RequestParam(name = "id", required = true) String id) {
        SmtYuyueList smtYuyueList = smtYuyueListService.getById(id);
        if (smtYuyueList == null) {
            return Result.error("信息不存在！");
        }
        //根据排期id查询出当前排期玩法的价格
        QueryWrapper<SmtTrkList> trkListQueryWrapper = new QueryWrapper<>();
        trkListQueryWrapper.eq("trk_id", smtYuyueList.getTrkId());
        trkListQueryWrapper.eq("dev_code", smtYuyueList.getDevCode());
        SmtTrkList trkList = smtTrkListService.getOne(trkListQueryWrapper);
        if (trkList != null) {
            smtYuyueList.setTrkPrice(BigDecimal.valueOf(trkList.getTrkPrice()));
        }

        //查询场次对应的人员
        QueryWrapper<SmtYuyueUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        queryWrapper.eq("status", 0);
        List<SmtYuyueUser> list = smtYuyueUserService.list(queryWrapper);
        smtYuyueList.setUserList(list);
        if (trkList != null) {
            smtYuyueList.setProductId(trkList.getProductId());
        }

        return Result.OK(smtYuyueList);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param smtYuyueList
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, SmtYuyueList smtYuyueList) {
        return super.exportXls(request, smtYuyueList, SmtYuyueList.class, "smt_yuyue_list");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, SmtYuyueList.class);
    }

}
